package com.imay.base.helper;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.util.Log;

import com.imay.base.util.NetworkUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Jeaman
 * 网络状态监听
 *
 */
public class NetworkHelper {
	private static final String TAG = NetworkHelper.class.getSimpleName();
	private static NetworkHelper instance;
	private Context mContext;
	private static BroadcastReceiver connectivityBroadcastReceiver;
	private static List<NetworkStateListener> mListener = new ArrayList<NetworkStateListener>();
	private static boolean mFirstReceive;
	
	private NetworkHelper(Context context) {
		mContext = context;
		registerConnectivityReceiver(context);
	}

	public static synchronized void init(Context context){
		if(instance == null){
			instance = new NetworkHelper(context.getApplicationContext());
		}
	}

	public static NetworkHelper getInstance() {
		return instance;
	}
	
	public void addListener(NetworkStateListener listener) {
		synchronized (mListener) {
			if (!mListener.contains(listener)) {
				mListener.add(listener);
			}
		}
	}
	
	public void removeListener(final NetworkStateListener listener) {
		synchronized (mListener) {
			mListener.remove(listener);
		}
	}

	public int getConnectionType() {
		return NetworkUtil.getNetworkType(mContext);
	}

	public boolean hasConnection() {
		return NetworkUtil.getNetworkType(mContext) != NetworkUtil.NO_NETWORK;
	}
	
    private static void registerConnectivityReceiver(Context context) {
        try {
        	connectivityBroadcastReceiver = new BroadcastReceiver() {
                public void onReceive(Context context, Intent intent) {
//					if (!mFirstReceive) {	//过滤启动时第一次触发事件
//						mFirstReceive = true;
//						return;
//					}
                    String action = intent.getAction();
                    if (!action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) {
                        return;
                    }
                    int networkType = NetworkUtil.getNetworkType(context);
                    try {
	                    synchronized (mListener) {
	                    	for (int i = 0; i < mListener.size(); i++) {
	                    		NetworkStateListener listener = mListener.get(i);
	                    		listener.onNetworkChange(networkType);
	                    	}
	            		}
                    } catch (Exception e) {
                    	Log.e(TAG, e.toString());
                    }
                }
        	};
            IntentFilter intentFilter = new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION);
            context.registerReceiver(connectivityBroadcastReceiver, intentFilter);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public void unregisterConnectivityReceiver() {
        try {
        	mContext.unregisterReceiver(connectivityBroadcastReceiver);
        } catch (Exception e) {
        	e.printStackTrace();
        }
    }
    
    public interface NetworkStateListener {
        /**
         * 返回网络变化状态
         * @hasDataConnection orgType  变化 chageToType  变化后
         */
        public void onNetworkChange(int networkType);

    }

}
